clear; clc;
load('data/recsys/movielen10m.mat');

para.tau = 1.01;
[row, col, val] = find(data);
[m, n] = size(data);

clear user item;

val = val - mean(val);
val = val/std(val);

idx = randperm(length(val));

traIdx = idx(1:floor(length(val)*0.5));
tstIdx = idx(ceil(length(val)*0.5): end);

clear idx;

traData = sparse(row(traIdx), col(traIdx), val(traIdx));
traData(size(data,1), size(data,2)) = 0;

para.test.row  = row(tstIdx);
para.test.col  = col(tstIdx);
para.test.data = val(tstIdx);
para.test.m = m;
para.test.n = n;

clear m n;

%% start testing
para.maxIter = 5000;
para.decay = 0.8;
para.tol = 1;

%% nuclear norm
lambda = 55;

% ActiveSubspace
method = 3;
t = tic;
% [ U, S, V, out{3} ] = ActiveSubspace( traData', lambda, para);
[ U, S, V, out{method,1} ] = mc_alt( traData, lambda, para );
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% FastMatComp
t = tic;
method = 4;
para.maxR = 300;
para.speedup = 1;
para.regType = 1;
[U, S, V, out{method,1}] = FastMatComp( traData, lambda, 1e+8, para );
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

clear U S V t;

% non-convex
para.tol = 15;
for i = 3:5
    
para.decay = 0.95;   
para.regType = i - 2;
para.maxR = 10;

switch(para.regType)
    case 1
        lambda = 90;
        theta1 = 2*lambda;
    case 2
        lambda = 10000;
        theta1 = sqrt(lambda);
    case 3
        lambda = 90;
        theta1 = 4;
end

% FAST
method = 2;
t = tic;
para.speedup = 1;
[U, S, V, out{method,i}] = FastMatComp( traData, lambda, theta1, para );
Time(method,i) = toc(t);
RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

end

%% Fixed Rank
estRank = 9;
method = 1;
t = tic;
[U, S, V, out{method,2} ] = SoftImputeALS( traData', 1e-7, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));

estRank = 50;
method = 2;
t = tic;
[U, S, V, out{method,2} ] = EOR1MP( traData, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

clear U S V method t estRank movies Age;
clear i col row theta1 traData traIdx tstIdx user val data item para lambda;


save('ml10m-rst3');
